package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.vo.CourseTeacherVO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.config.LocalDateTimeConfig;
import com.xuecheng.content.convert.TeachplanConvert;
import com.xuecheng.content.entity.CourseTeacher;
import com.xuecheng.content.entity.CourseTeacherDTO;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {
    @Autowired
    private CourseBaseService courseBaseService;

    @Autowired
    private CourseTeacherMapper courseTeacherMapper;
    @Override
    public List<CourseTeacherVO> queryTeachersByCourseId(Long courseBaseId, Long companyId) {

        /**
         *   0. 是否开启事务
         *   1. 判断关键数据
         *   2. 判断业务数据
         *   3. 业务操作
         *   4. 返回对象
         */
        if(ObjectUtils.isEmpty(courseBaseId)||ObjectUtils.isEmpty(companyId)){
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }

      //  2. 判断业务数据
        CourseBaseDTO dto = courseBaseService.getCourseById(courseBaseId, companyId);
      // 如果是空的
        if(ObjectUtils.isEmpty(dto)){
            throw new RuntimeException("没有此课程。");
        }
        // 不是空的
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(CourseTeacher::getCourseId,courseBaseId);
        List<CourseTeacher> dtos = courseTeacherMapper.selectList(queryWrapper);
        List<CourseTeacherVO> list = TeachplanConvert.INSTANCE.tdtos2tvos(dtos);

        return list;
    }

    @Override
    @Transactional
    public CourseTeacherVO createOrModifyTeacher(CourseTeacherVO vo, Long companyId) {

        if(ObjectUtils.isEmpty(vo.getCourseId())||
           //ObjectUtils.isEmpty(vo.getPhotograph())||
           ObjectUtils.isEmpty(vo.getPosition())||
           ObjectUtils.isEmpty(vo.getTeacherName())||
           ObjectUtils.isEmpty(companyId)){
            ExceptionCast.cast(CommonErrorCode.E_100101);

        }

        if(ObjectUtils.isEmpty(vo.getCourseTeacherId())){
            vo.setCourseTeacherId(createTeacher(vo,companyId));
        }else{
            modifyTeacher(vo,companyId);
        }
        CourseTeacher dto = this.getById(vo.getCourseTeacherId());
   //     dto.setId(dto.getId()%1000);


        return TeachplanConvert.INSTANCE.ten2tvo(dto);
    }

    @Override
    @Transactional
    public void removeTeacher(Long courseId, Long courseTeacherId, Long companyId) {
        // 判断关键数据
        if(ObjectUtils.isEmpty(courseId)||
                ObjectUtils.isEmpty(courseTeacherId)||
                       ObjectUtils.isEmpty(companyId) ){
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        //判断业务数据
//        CourseBaseDTO course = courseBaseService.getCourseById(courseId, companyId);
//
//        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(CourseTeacher::getId, courseTeacherId);
//        Integer integer = courseTeacherMapper.selectCount(queryWrapper);
//
//        if(integer>0){
//            ExceptionCast.cast(ContentErrorCode);
//        }
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,courseId);
        queryWrapper.eq(CourseTeacher::getId,courseTeacherId);
        int delete = courseTeacherMapper.delete(queryWrapper);
        if(delete==0){
            ExceptionCast.cast(ContentErrorCode.E_1200505);
        }
    }

    private void modifyTeacher(CourseTeacherVO vo, Long companyId) {
//        修改
//        2.业务数据
        Long courseId = vo.getCourseId();
        CourseBaseDTO course = courseBaseService.getCourseById(courseId, companyId);
        if(ObjectUtils.isEmpty(course)){
            ExceptionCast.cast(CommonErrorCode.E_100117);
        }

        if(course.getStatus().equals("0")){
            throw new RuntimeException("课程已删除");
        }

        CourseTeacher dto= CourseTeacher.builder().courseId(vo.getCourseId())
                .coursePubId(null)
                .id(vo.getCourseTeacherId())
                //    .createDate(new LocalDateTimeConfig())
                .introduction(vo.getIntroduction())
                .photograph(vo.getPhotograph())
                .position(vo.getPosition())
                .teacherName(vo.getTeacherName()).build();

        int  update = courseTeacherMapper.updateById(dto);
        if(update==0){
            ExceptionCast.cast(ContentErrorCode.E_1200501);
        }


    }

    private Long createTeacher(CourseTeacherVO vo, Long companyId) {
        Long courseId = vo.getCourseId();
        CourseBaseDTO course = courseBaseService.getCourseById(courseId, companyId);
        if(ObjectUtils.isEmpty(course)){
            ExceptionCast.cast(CommonErrorCode.E_100117);
        }

        if(course.getStatus().equals("0")){
            throw new RuntimeException("课程已删除");
        }

        CourseTeacher dto= CourseTeacher.builder().courseId(vo.getCourseId())
                .coursePubId(null)
                .id(vo.getCourseTeacherId())
            //    .createDate(new LocalDateTimeConfig())
                .introduction(vo.getIntroduction())
                .photograph(vo.getPhotograph())
                .position(vo.getPosition())
                .teacherName(vo.getTeacherName()).build();

        int save = courseTeacherMapper.insert(dto);

        if(save==0){
            ExceptionCast.cast(ContentErrorCode.E_1200501);
        }
        return dto.getId();
    }


}
